package com.jason.apps.dashboard;

import java.util.ArrayList;
import java.util.HashMap;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.app.Activity;
import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.actionbarsherlock.app.SherlockFragment;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.apps.essentials.ConsumeWebService;
import com.apps.realtyportal.R;
import com.forimageandlist.LeadAdapter;
import com.forimageandlist.SoldAdapter;

public class LatestSold extends SherlockFragment {

	ConsumeWebService c;
	
	final static ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
	
	HashMap<String, String> map;
	private ListView list;
	SherlockFragmentActivity fa;
	SoldAdapter adapter;
	
	
	public static final String KEY_NAME = "name";
	public static final String KEY_DESCRIPTION = "description";
	public static final String KEY_PRICE = "price";
	public static final String KEY_LATITUDE = "latitude";
	public static final String KEY_LONGITUDE = "longitude";
	public static final String KEY_ADDRESS = "address";
	public static final String KEY_TYPE = "type";
	public static final String KEY_IMAGE = "image";
	
	public void displayList(){
		c = new ConsumeWebService();
		 String data = c.getSoldProperty();
		 
		 try {

				JSONArray array = new JSONArray(data);
				
				for (int i = 0; i < array.length(); i++) {
					map = new HashMap<String, String>();
					JSONObject e = array.getJSONObject(i);

					map.put("property_name", e.getString(KEY_NAME));
					map.put("property_description", e.getString(KEY_DESCRIPTION));
					map.put("price", e.getString(KEY_PRICE));
					map.put("latitude", e.getString(KEY_LATITUDE));
					map.put("longitude", e.getString(KEY_LONGITUDE));
					map.put("address", e.getString(KEY_ADDRESS));
					map.put("type", e.getString(KEY_TYPE));
					map.put("image", e.getString(KEY_IMAGE));
					
					mylist.add(map);
				}
			} catch (JSONException e) {
				Log.e("log_tag", "Error parsing data " + e.toString()+"diri jud");
			}finally{
				
			}
	}
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.listforeditproperty, container, false);
		fa= (SherlockFragmentActivity) super.getActivity();
		
		mylist.clear();
		list=(ListView)view.findViewById(R.id.list_editproperty);
		adapter=new SoldAdapter(getActivity(),mylist);
		new loadList().execute();
		
		return view;
    }
	public class loadList extends AsyncTask<Void, Void, Void> {

	    private final ProgressDialog dialog = new ProgressDialog(fa);

	    protected void onPreExecute() {

	            this.dialog.setMessage("Loading...");
	            this.dialog.show();
	    }


	    protected Void doInBackground(final Void... unused) {
	    	//getAllLabels();
	    	displayList();
	        return null; 
	    }
	    protected void onPostExecute(Void result) {
	    	//loadSpinner(agentspinner);
			list.setAdapter(adapter);
	    	if (this.dialog!=null) {
	            this.dialog.dismiss();
	            }  
	     }
	  }


}
